/**
 * 
 */
package com.smarthome.web.property.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.smarthome.common.tools.BossCachedKeysUtil;
import com.smarthome.common.tools.SpringUtils;
import com.smarthome.core.area.AreaProvinceService;
import com.smarthome.core.area.impl.AreaProvinceServiceImpl;
import com.smarthome.domain.bean.ShAreaCity;
import com.smarthome.domain.bean.ShAreaDistrict;
import com.smarthome.domain.bean.ShAreaProvince;

/**
 * @author dhc
 * @date 2015年10月13日 下午5:13:49
 * @title 获取区域Util类
 */
public class AreaUtil {

    private static AreaProvinceService areaProvinceService = SpringUtils.getBean(AreaProvinceServiceImpl.class);
    private static Map<String, Object> localCache = new HashMap<String, Object>();
    static { // 初始化缓存
        init();
    }

    private static void init() {
        List<ShAreaProvince> plist = areaProvinceService.queryShAreaProvinces(null);
        areaProvinceService.addParam(plist);
        localCache.put(BossCachedKeysUtil.AREA_PROVINCE, plist);

        if (null != plist) {
            List<ShAreaCity> allCity = new ArrayList<ShAreaCity>();
            List<ShAreaDistrict> allDistricts = new ArrayList<ShAreaDistrict>();
            for (ShAreaProvince p : plist) {
                if (null == p)
                    continue;
                localCache.put(BossCachedKeysUtil.getKey(BossCachedKeysUtil.AREA_PROVINCE, p.getId().toString()), p);
                List<ShAreaCity> clist = p.getCitys();
                if (null == clist)
                    continue;
                allCity.addAll(clist);
                for (ShAreaCity city : clist) {
                    if (null == city)
                        continue;
                    localCache.put(BossCachedKeysUtil.getKey(BossCachedKeysUtil.AREA_CITY, city.getId().toString()), city);
                    List<ShAreaDistrict> districts = city.getDistricts();
                    if (null == districts)
                        continue;
                    allDistricts.addAll(districts);
                    for (ShAreaDistrict d : districts) {
                        localCache.put(BossCachedKeysUtil.getKey(BossCachedKeysUtil.AREA_DISTRICTS, d.getId().toString()), d);
                    }
                }
            }
            localCache.put(BossCachedKeysUtil.AREA_CITY, allCity);
            localCache.put(BossCachedKeysUtil.AREA_DISTRICTS, allDistricts);
        }

    }

    /**
     * @title 获取所有的区县
     * @createTime 2015年10月13日 下午5:16:43
     * @author dhc
     */
    public static List<ShAreaProvince> getAllProvince() {
        List list = (List) localCache.get(BossCachedKeysUtil.AREA_PROVINCE);
        return list;
    }

    /**
     * @title 根据id，获取
     * @createTime 2015年10月13日 下午5:55:18
     * @author dhc
     */
    public static ShAreaProvince getProvince(Long id) {
        return (ShAreaProvince) localCache.get(BossCachedKeysUtil.getKey(BossCachedKeysUtil.AREA_PROVINCE, id.toString()));
    }

    /**
     * @title 获取所有的城市列表
     * @createTime 2015年10月13日 下午5:57:01
     * @author dhc
     */
    public static List<ShAreaCity> getAllCity() {
        return (List) localCache.get(BossCachedKeysUtil.AREA_CITY);
    }

    /**
     * @title 根据id，获取城市列表
     * @createTime 2015年10月13日 下午5:59:38
     * @author dhc
     */
    public static ShAreaCity getCity(Long id) {
        return (ShAreaCity) localCache.get(BossCachedKeysUtil.getKey(BossCachedKeysUtil.AREA_CITY, id.toString()));
    }

    /**
     * @title
     * @createTime 2015年10月13日 下午6:02:34
     * @author dhc
     */
    public static List<ShAreaDistrict> getAllDistrict() {
        return (List) localCache.get(BossCachedKeysUtil.AREA_DISTRICTS);
    }

    /**
     * @title
     * @createTime 2015年10月13日 下午6:02:30
     * @author dhc
     */
    public static ShAreaDistrict getDistrict(Long id) {
        return (ShAreaDistrict) localCache.get(BossCachedKeysUtil.getKey(BossCachedKeysUtil.AREA_DISTRICTS, id.toString()));
    }

    /**
     * @title 刷新缓存
     * @createTime 2015年10月13日 下午5:26:35
     * @author dhc
     */
    public static boolean reflashAll() {
        boolean isTrue = false;
        try {
            init();
            isTrue = true;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return isTrue;
    }
}
